package 富婆梦.第一阶段.第七周IO流;

import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * @ProjectName: java-learning
 * @Package: 富婆梦.第一阶段.第七周
 * @ClassName: TestRandomAccessFile
 * @Author: hemengjin
 * @Description: IO知识点，随机操作文件的类：RandomAccessFile
 * @Date:2021/9/10 8:33
 * @Version: 1.0
 */
public class TestRandomAccessFile {
    static String file = "rtast.dat";

    static void display() throws IOException {
        RandomAccessFile rf = new RandomAccessFile(file, "r");
        for (int i = 0; i < 7; i++) {
            System.out.println("value" + i + ":" + rf.readDouble());
            System.out.println(rf.readUTF());
            rf.close();
        }
    }

    public static void main(String[] args) {
        try {
            RandomAccessFile rf = new RandomAccessFile(file, "rw");
            for (int i = 0; i < 7; i++) {
                rf.writeDouble(i * 1.41);
                rf.writeUTF("haha");
                //rf.writeUTF("The end of file");
                rf.close();
                display();
                rf = new RandomAccessFile(file, "rw");
                rf.seek(5 * 8);
                rf.writeDouble(47.0001);
                rf.close();
                display();

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
